/**
 * GridContainer.as
 * Copyright 2014 Qunhe Tech, all rights reserved.
 * Qunhe PROPRIETARY/CONFIDENTIAL, any form of usage is subject to approval.
 */
package org.bridge.flash.hi2048.view
{
    import flash.events.Event;
    import flash.events.KeyboardEvent;

    import mx.core.UIComponent;

    import org.bridge.flash.hi2048.model.core.Matrix;
    import org.bridge.flash.hi2048.view.event.OperationEvent;

    /**
     * @author zhangshuai
     */
    public class GridPanel extends UIComponent
    {
        public static const DIM:int = 4;

        private var mGrids:Array = new Array;

        public function GridPanel()
        {
            init();
            render();
        }

        private function init():void
        {
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        protected function onAddedToStage(event:Event):void
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        }

        protected function onKeyDown(event:KeyboardEvent):void
        {
            var oe:OperationEvent = new OperationEvent(OperationEvent.OPERATE);
            oe.keyCode = event.keyCode;
            dispatchEvent(oe);
        }

        private function render():void
        {
            const len:int = DIM * Grid.GRID_SIDE_LENGTH + (DIM + 1) * Grid.GRID_PADDING;
            graphics.beginFill(0xADADAD);
            graphics.drawRoundRect(0, 0, len, len, 20);
            graphics.endFill();
        }

        public function initMatrix(matrix:Matrix):void
        {
            for (var i:int = 0; i < DIM; i++)
            {
                var row:Array = new Array;
                for (var j:int = 0; j < DIM; j++)
                {
                    var grid:Grid = new Grid(i, j, matrix.fetchOne(i, j));
                    addChild(grid);
                    row.push(grid);
                }
                mGrids.push(row);
            }
        }
        
        public function 
    }
}
